package tk.mybatis.springboot.Timer;

import com.virjar.dungproxy.client.ippool.IpPoolHolder;
import com.virjar.dungproxy.client.ippool.config.DungProxyContext;
import com.virjar.dungproxy.client.ippool.strategy.impl.JSONFileAvProxyDumper;
import com.virjar.dungproxy.client.ippool.strategy.impl.WhiteListProxyStrategy;
import com.virjar.vscrawler.core.VSCrawler;
import com.virjar.vscrawler.core.VSCrawlerBuilder;
import com.virjar.vscrawler.core.net.proxy.DefaultIPPool;
import com.virjar.vscrawler.core.net.proxy.strategy.ProxyStrategy;
import com.virjar.vscrawler.core.seed.Seed;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import tk.mybatis.springboot.service.YService;
import tk.mybatis.springboot.spider.ProcessorTest;
import tk.mybatis.springboot.spider.pipeline.YPipeline;

import java.util.List;


@Component
public class Job {

    @Autowired
    private YPipeline yPipeline;

    @Autowired
    private YService yService;



    /**
     * 米思米
     */
//    @Scheduled(cron = "0 49 13 15 * ?")
    public void msm_timer() {

        WhiteListProxyStrategy whiteListProxyStrategy = new WhiteListProxyStrategy();
        whiteListProxyStrategy.addAllHost("cn.misumi-ec.com");
        JSONFileAvProxyDumper jsonFileAvProxyDumper = new JSONFileAvProxyDumper();
        jsonFileAvProxyDumper.setDumpFileName("availableProxy.json");
        DungProxyContext dungProxyContext = DungProxyContext.create().setNeedProxyStrategy(whiteListProxyStrategy)
                .setAvProxyDumper(jsonFileAvProxyDumper).setPoolEnabled(true);
        dungProxyContext.getGroupBindRouter().buildCombinationRule("cn.misumi-ec.com:.*misumi-ec.*");
        IpPoolHolder.init(dungProxyContext);

        System.out.println("========爬虫【启动】=========");
//        Spider.create(new YPageProcessor())
//                .addPipelince(yPipeline)
//                .setDownloader(new DungProxyDownloader())
//                .addUrl("https://cn.misumi-ec.com/vona2/mech/M0100000000/M0104000000/")
//                .thread(10)
//                .runAsync();
    }


    /**
     * 阿母工业
     */
    @Scheduled(cron = "0 23 14 23 * ?")
    public void imow_timer() {
        try {
            IpPoolHolder.init(DungProxyContext.create().setPoolEnabled(true));
            VSCrawler vsCrawler = VSCrawlerBuilder.create()
                    .setSlowStart(true)
                    .setCrawlerName("IMOW")
                    .setProcessor(new ProcessorTest())
                    .setIpPool(new DefaultIPPool())
                    .setProxyStrategy(ProxyStrategy.REQUEST)
                    .addPipeline(yPipeline)
                    .build();
            // 清空历史爬取数据,或者会断点续爬
            vsCrawler.clearTask();
            vsCrawler.pushSeed("http://www.imow.cn");
            // 开始爬虫
            vsCrawler.start();
        }catch (Exception e){
            System.err.println("爬虫启动异常");
        }
    }


    @Scheduled(cron = "0 */40 * * * ?")
    public void getErrorLink_timer() {
        System.err.println("###################");
        System.err.println("#######ERROR#######");
        System.err.println("###################");
        List<String> links = yService.getErrorLink();
        if (links.size() > 0) {
//        IpPoolHolder.init(DungProxyContext.create().setPoolEnabled(true));
            VSCrawler vsCrawler = VSCrawlerBuilder.create()
                    .setSlowStart(true)
                    .setSessionPoolCoreSize(10)
                    .setSessionPoolInitialSize(30)
                    .setCrawlerName("ERRORLINK")
                    .setProcessor(new ProcessorTest())
//                .setIpPool(new DefaultIPPool())
//                .setProxyStrategy(ProxyStrategy.REQUEST)
                    .addPipeline(yPipeline)
                    .build();
            vsCrawler.clearTask();
            System.out.println("********************** " + vsCrawler.getState());
            System.err.println("###### 获取到重试链接 ###### " + links.size());

            for (String link : links) {
                System.out.println("###### 重试链接 ######" + link);
                vsCrawler.pushSeed(new Seed(link));
            }
            if (vsCrawler.isAlive()) {
                System.err.println("currentVs is alive");
            }
            vsCrawler.start();
            // 删除重试过的链接
            if (yService.setRetry()) {
                System.err.println("update error_link succeed");
            }
        } else {
            System.out.println("无重试链接-不执行任务");
        }
    }
}